
// AEFW 
function set_button(){
	if( jQuery.ui ) {
		$('[button]').each(function(index,element){
			$(element).button({
				icons: {
					primary: $(this).attr('button'),
					secondary: $(this).attr('button-secondary')
				}
			})
		});
	}
}
function set_button_icon(){
	if( jQuery.ui ) {
		$('[button-icon]').each(function(index,element){
			$(element).button({
				text: false,
				icons: {
					primary: $(this).attr('button-icon'),
					secondary: $(this).attr('button-secondary')
				}
			})
		});
	}
}
function set_ui_tooltip(){
	if( jQuery.ui ) {
		var elemtooltip = '';
		elemtooltip = 'input[title],img[alt],a[title],[tooltip]';
		if($(elemtooltip).length > 0){
			$( document ).tooltip({
				items: elemtooltip,
				/*
				track: function(){
					var element = $( this );
					if ( element.is( "track-cursor" ) ) {
						return true;
					}
					return false;
				},
				//*/
				position: {
					my: "center bottom-20",
					at: "center top",
					using: function( position, feedback ) {
						$( this ).css( position );
						$( "<div>" )
						.addClass( "toolbarrow" )
						.addClass( feedback.vertical )
						.addClass( feedback.horizontal )
						.appendTo( this );
					}
				},
				show: {
					effect: "fadeIn",
					duration: "slow",
				},
				hide: {
					effect: "slideUp",
					duration: "slow",
				},
				content: function() {
					var element = $( this );
					if ( element.is( "[data-geo]" ) ) {
						var text = element.text();
						return "<img class='map' alt='" + text +
						"' src='http://maps.google.com/maps/api/staticmap?" +
						"zoom=11&size=350x350&maptype=terrain&sensor=false&center=" +
						text + "'>";
					}
					if ( element.is( "[title]" ) ) {
						return element.attr( "title" );
					}
					if ( element.is( "img" ) ) {
						return element.attr( "alt" );
					}
					if ( element.is( "[tooltip]" ) ) {
						return element.attr( "tooltip" );
					}
				}
			});
			$( document ).click(function(e) {
				if( $('.ui-tooltip').length > 0 )
					$('.ui-tooltip').remove();
			});
		}
	}
}

// Show PopUp
function PopUp(option) {
	option.url = ( typeof(option.url) == "undefined" ) ? '' : option.url ;
	option.width = ( typeof(option.width) == "undefined" ) ? 800 : option.width ;
	option.height = ( typeof(option.height) == "undefined" ) ? 600 : option.height ;
	option.head = ( typeof(option.head) == "undefined" ) ? '' : option.head ;
	option.text = ( typeof(option.text) == "undefined" ) ? '' : option.text ;
	option.goprint = ( typeof(option.goprint) == "undefined" ) ? false : option.goprint ;

	var headContent = document.getElementsByTagName('head')[0].innerHTML;
//	var html_open = "<html><head>" + headContent + "</head><body>";
	var html_open = "<html><head>" + option.head + "</head><body>";
	var html_close = "</body></html>";
	//goprint = false;

	if(window.innerWidth){
		LeftPosition = (window.innerWidth - option.width) / 2;
		TopPosition = ((window.innerHeight - option.height) / 4) - 50;
	}else{
		LeftPosition = (parseInt(window.screen.width) - option.width) / 2;
		TopPosition = ((parseInt(window.screen.height) - option.height) / 2) - 50;
	}
	attr = 'location=no,menubar=no,scrollbars=yes,status=yes,resizable=no,width=' + option.width + ',height=' +
	option.height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' + TopPosition + '';

	popWin = window.open(option.url, '', attr);

	if( option.url=='' ) {
		var popText = popWin.document;
		popText.write(html_open);
		popText.write(option.text);
		popText.write(html_close);
		popText.close();

		popWin.focus();
		if(option.goprint) popWin.body.onload = popWin.print();
	}
	return false;
}
//<a href="#" onclick="popup('','800','600','idareaprint',true); return false;">text link</a>

function asset_dir_url() {
	src = null;
	sc = document.getElementsByTagName("script");
	for(idx = 0; idx < sc.length; idx++) {
		var regex = /script\.js$/gi;
		s = sc.item(idx);
		if(s.src && s.src.match(regex)) {
			src = s.src.replace(regex, '');
		}
	}
	return src;
}

// END AEFW

$(document).ready(function(e) {
	set_button();
	set_button_icon();
	set_ui_tooltip();
});
